package org.yaac.client.widget.stat;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.google.gwt.dom.client.Style;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlowPanel;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class KindMultiFilterPanel extends FlowPanel {

	public static interface OnChangeHandler {
		void onChange(Set<KindFilterChoice> vals);
	}
	
	private CheckBox countCb;
	
	private CheckBox sizeCb;
	
	private CheckBox countRootCb;
	
	private CheckBox sizeRootCb;
	
	private CheckBox countNonRootCb;
	
	private CheckBox sizeNonRootCb;
	
	private List<OnChangeHandler> handlers;
	
	/**
	 * 
	 */
	public KindMultiFilterPanel() {
		handlers = new LinkedList<OnChangeHandler>();
		
		// count all
		countCb = new CheckBox("Count(All)");	// TODO : i18n
		countCb.getElement().getStyle().setFloat(Style.Float.LEFT);
		countCb.setValue(true);	
		countCb.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				onChange();
			}
		});
		this.add(countCb);
		
		// size all
		sizeCb = new CheckBox("Size(All)");	// TODO : i18n
		sizeCb.getElement().getStyle().setFloat(Style.Float.LEFT);
		sizeCb.setValue(true);
		sizeCb.addValueChangeHandler(new ValueChangeHandler<Boolean>() {

			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				onChange();
			}
		});
		this.add(sizeCb);
		
		// count root
		countRootCb = new CheckBox("Count(Root)");	// TODO : i18n
		countRootCb.getElement().getStyle().setFloat(Style.Float.LEFT);
		countRootCb.setValue(true);	
		countRootCb.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				onChange();
			}
		});
		this.add(countRootCb);
		
		// size root
		sizeRootCb = new CheckBox("Size(Root)");	// TODO : i18n
		sizeRootCb.getElement().getStyle().setFloat(Style.Float.LEFT);
		sizeRootCb.setValue(true);
		sizeRootCb.addValueChangeHandler(new ValueChangeHandler<Boolean>() {

			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				onChange();
			}
		});
		this.add(sizeRootCb);
		
		// count non root
		countNonRootCb = new CheckBox("Count(Non Root)");	// TODO : i18n
		countNonRootCb.getElement().getStyle().setFloat(Style.Float.LEFT);
		countNonRootCb.setValue(true);	
		countNonRootCb.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				onChange();
			}
		});
		this.add(countNonRootCb);
		
		// size non root
		sizeNonRootCb = new CheckBox("Size(Non Root)");	// TODO : i18n
		sizeNonRootCb.setValue(true);
		sizeNonRootCb.addValueChangeHandler(new ValueChangeHandler<Boolean>() {

			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				onChange();
			}
		});
		this.add(sizeNonRootCb);
	}
	
	/**
	 * @return
	 */
	public Set<KindFilterChoice> selectedValues() {
		Set<KindFilterChoice> result = new HashSet<KindFilterChoice>();
		
		if (countCb.getValue()) {
			result.add(KindFilterChoice.COUNT_ALL);
		} 
		
		if (sizeCb.getValue()) {
			result.add(KindFilterChoice.SIZE_ALL);
		}
		
		if (countRootCb.getValue()) {
			result.add(KindFilterChoice.COUNT_ROOT);
		}
		
		if (sizeRootCb.getValue()) {
			result.add(KindFilterChoice.SIZE_ROOT);
		}
		
		if (countNonRootCb.getValue()) {
			result.add(KindFilterChoice.COUNT_NON_ROOT);
		}
		
		if (sizeNonRootCb.getValue()) {
			result.add(KindFilterChoice.SIZE_NON_ROOT);
		}
		
		return result;
	}
	
	public void addOnChangeHandler(OnChangeHandler handler) {
		this.handlers.add(handler);
	}
	
	private void onChange() {
		for (OnChangeHandler handler : handlers) {
			handler.onChange(selectedValues());
		}
	}
}
